<template>
  <div class="home">
    <!-- 顶部导航栏 -->
    <Navbar />

    <!-- 轮播图 -->
    <div class="carousel-section">
      <el-carousel height="400px" :interval="4000">
        <el-carousel-item v-for="(banner, index) in banners" :key="index">
          <div class="banner-item" :style="{ backgroundColor: banner.bgColor }">
            <img :src="banner.image" :alt="banner.title" class="banner-image" />
            <div class="banner-content">
              <h2>{{ banner.title }}</h2>
              <p>{{ banner.desc }}</p>
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>

    <!-- 分类导航 -->
    <div class="category-section">
      <div class="container">
        <h2 class="section-title">商品分类</h2>
        <div class="category-grid">
          <div 
            v-for="category in categories" 
            :key="category.categoryId"
            class="category-item"
            @click="goToCategory(category.categoryId)"
          >
            <div class="category-icon">
              <el-icon :size="40"><Box /></el-icon>
            </div>
            <span class="category-name">{{ category.categoryName }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 推荐商品区域 -->
    <div class="recommend-section">
      <div class="container">
        <h2 class="section-title">推荐商品</h2>
        
        <div class="goods-grid" v-loading="loading">
          <!-- 空状态提示 -->
          <el-empty 
            v-if="!loading && goodsList.length === 0" 
            description="暂无推荐商品"
            :image-size="200"
            class="empty-state"
          />
          
        <!-- 商品卡片 -->
        <el-card 
          v-for="goods in goodsList" 
          :key="goods.goodsId"
          class="goods-card"
          :body-style="{ padding: '0px' }"
          shadow="hover"
          @click="goToDetail(goods.goodsId)"
        >
          <img :src="getImageUrl(goods.image)" class="goods-image" />
          <div class="goods-info">
            <h3 class="goods-name">{{ goods.goodsName }}</h3>
            <p class="goods-desc">{{ goods.goodsDesc }}</p>
            <div class="goods-footer">
              <span class="goods-price">¥{{ goods.goodsPrice }}</span>
              <span class="goods-stock">库存: {{ goods.sellStatus }}</span>
            </div>
          </div>
        </el-card>
        </div>

        <!-- 分页 -->
        <el-pagination
          v-if="total > 0"
          class="pagination"
          v-model:current-page="pageNum"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getAllCategories } from '@/api/category'
import { getRecommendGoods } from '@/api/goods'
import { ElMessage } from 'element-plus'
import { Box } from '@element-plus/icons-vue'
import Navbar from '@/components/Navbar.vue'
import { getFileUrl } from '@/api/file'

const router = useRouter()

// 轮播图数据
const banners = ref([
  {
    image: 'https://picsum.photos/1200/400?random=1',
    title: '精选纪念品',
    desc: '珍藏美好回忆',
    bgColor: '#ff6b6b'
  },
  {
    image: 'https://picsum.photos/1200/400?random=2',
    title: '特色商品',
    desc: '独一无二的选择',
    bgColor: '#4ecdc4'
  },
  {
    image: 'https://picsum.photos/1200/400?random=3',
    title: '热门推荐',
    desc: '人气爆款商品',
    bgColor: '#45b7d1'
  }
])

// 分类数据
const categories = ref([])

// 商品列表数据
const goodsList = ref([])
const loading = ref(false)
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 获取分类列表
const loadCategories = async () => {
  try {
    const data = await getAllCategories()
    categories.value = data || []
  } catch (error) {
    console.error('获取分类失败:', error)
  }
}

// 获取推荐商品列表
const loadRecommendGoods = async () => {
  loading.value = true
  try {
    const data = await getRecommendGoods({
      pageNum: pageNum.value,
      pageSize: pageSize.value
    })
    console.log('推荐商品数据:', data)
    goodsList.value = data.list || []
    total.value = data.total || 0
    
    if (goodsList.value.length === 0) {
      console.warn('没有推荐商品数据')
    }
  } catch (error) {
    console.error('获取推荐商品失败:', error)
    ElMessage.error('获取推荐商品失败，请检查后端服务是否启动')
  } finally {
    loading.value = false
  }
}

// 跳转到分类商品页
const goToCategory = (categoryId) => {
  router.push(`/category/${categoryId}`)
}

// 跳转到商品详情页
const goToDetail = (goodsId) => {
  router.push(`/goods/${goodsId}`)
}

// 分页相关
const handleSizeChange = (val) => {
  pageSize.value = val
  loadRecommendGoods()
}

const handleCurrentChange = (val) => {
  pageNum.value = val
  loadRecommendGoods()
}

// 获取图片URL
const getImageUrl = (imagePath) => {
  return getFileUrl(imagePath)
}

// 页面加载时
onMounted(() => {
  loadCategories()
  loadRecommendGoods()
})
</script>

<style scoped>
.home {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f5f5;
  margin: 0;
  padding: 0;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 轮播图 */
.carousel-section {
  width: 100%;
  background: #fff;
  margin: 0;
  padding: 0;
}

.carousel-section :deep(.el-carousel) {
  margin: 0;
}

.carousel-section :deep(.el-carousel__container) {
  margin: 0;
}

.banner-item {
  width: 100%;
  height: 400px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.banner-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0.8;
}

.banner-content {
  position: relative;
  z-index: 1;
  text-align: center;
  color: white;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.banner-content h2 {
  font-size: 48px;
  font-weight: bold;
  margin: 0 0 15px 0;
}

.banner-content p {
  font-size: 24px;
  margin: 0;
}

/* 分类区域 */
.category-section {
  background: white;
  padding: 40px 0;
}

.category-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 20px;
}

.category-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: #f9f9f9;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.category-item:hover {
  background: #ff4d4f;
  color: white;
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(255, 77, 79, 0.3);
}

.category-icon {
  margin-bottom: 10px;
}

.category-item:hover .category-icon {
  color: white;
}

.category-name {
  font-size: 16px;
  font-weight: 500;
}

/* 推荐商品区域 */
.recommend-section {
  background: #f5f5f5;
  padding: 40px 0;
}

.section-title {
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 30px;
  color: #333;
  position: relative;
  padding-left: 15px;
}

.section-title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 5px;
  height: 28px;
  background: #ff4d4f;
  border-radius: 3px;
}

.goods-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 24px;
  margin-bottom: 30px;
  min-height: 300px;
}

.empty-state {
  grid-column: 1 / -1;
  padding: 60px 0;
}

.goods-card {
  cursor: pointer;
  transition: all 0.3s;
  border-radius: 8px;
  overflow: hidden;
}

.goods-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.goods-image {
  width: 100%;
  height: 220px;
  object-fit: cover;
}

.goods-info {
  padding: 16px;
}

.goods-name {
  font-size: 16px;
  font-weight: bold;
  margin: 0 0 8px 0;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.goods-desc {
  font-size: 14px;
  color: #666;
  margin: 0 0 12px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.goods-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.goods-price {
  font-size: 20px;
  font-weight: bold;
  color: #ff4d4f;
}

.goods-stock {
  font-size: 12px;
  color: #999;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 40px;
  padding-bottom: 40px;
}

/* 响应式 */
@media (max-width: 768px) {
  .banner-content h2 {
    font-size: 32px;
  }

  .banner-content p {
    font-size: 18px;
  }

  .category-grid {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 12px;
  }

  .goods-grid {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 16px;
  }

  .section-title {
    font-size: 22px;
  }
}
</style>
